package com.inforich.app.api.controller.carManage;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.inforich.app.api.dto.DriverDTO;
import com.inforich.app.api.service.IAppDriverService;
import com.inforich.app.component.LoginUser;
import com.inforich.app.component.LoginUserApp;
import com.inforich.app.component.PreAppRole;
import com.inforich.appManage.service.IContactService;
import com.inforich.carManage.domain.Driver;
import com.inforich.carManage.service.IDriverService;
import com.inforich.common.core.domain.AjaxResult;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 车辆管理
 * @author: HJM
 * @date: 2022/10/31
 */
@RestController
@RequestMapping("/h5api/carManage/driver")
public class AppDriverController {

  @Autowired
  private IDriverService driverService;
  @Autowired
  private IContactService contactService;
  @Autowired
  private IAppDriverService appDriverService;

  @PreAppRole("transport_company_admin")
  @GetMapping("/getCompanyDriverList")
  public AjaxResult getCompanyDriverList(@LoginUser LoginUserApp loginUserApp) {
    // 公司ID
    String transportCompanyId = contactService.getTransportCompanyIdByAppUserId(
        loginUserApp.getId());
    if (StrUtil.isBlank(transportCompanyId)) {
      return AjaxResult.error("缺少公司信息");
    }
    List<Driver> list = driverService.list(new LambdaQueryWrapper<Driver>()
        .eq(Driver::getTransportCompanyId, transportCompanyId));
    return AjaxResult.success(list);
  }

  /**
   * 添加车辆
   */
  @PreAppRole("transport_company_admin")
  @PostMapping("/add")
  public AjaxResult addDriver(@LoginUser LoginUserApp loginUserApp,
      @Validated @RequestBody DriverDTO dto) {
    return appDriverService.addDriver(loginUserApp, dto);
  }

  /**
   * 修改车辆
   */
  @PreAppRole("transport_company_admin")
  @PostMapping("/update")
  public AjaxResult updateDriver(@LoginUser LoginUserApp loginUserApp,
      @RequestBody DriverDTO dto) {
    return appDriverService.updateDriver(loginUserApp, dto);
  }

  /**
   * 删除车辆
   */
  @PreAppRole("transport_company_admin")
  @GetMapping("/remove")
  public AjaxResult removeDriver(@LoginUser LoginUserApp loginUserApp, Long id) {
    return appDriverService.removeDriver(loginUserApp, id);
  }

  /**
   * 车辆详情
   */
  @PreAppRole("transport_company_admin")
  @GetMapping("/query")
  public AjaxResult getDriver(@LoginUser LoginUserApp loginUserApp, Long id) {
    return appDriverService.getDriver(loginUserApp, id);
  }

}
